package com.hiyee.platform.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.hiyee.platform.exception.GraceException;
import com.hiyee.platform.mapper.AmapLieYingTerminalMapper;
import com.hiyee.platform.mapper.AmapLieYingTraceMapper;
import com.hiyee.platform.mapper.AmapLieyingServiceMapper;
import com.hiyee.platform.pojo.AmapLieYingTerminal;
import com.hiyee.platform.pojo.AmapLieyingService;
import com.hiyee.platform.pojo.AmapLieyingTrace;
import com.hiyee.platform.pojo.bo.*;
import com.hiyee.platform.response.PageListResult;
import com.hiyee.platform.response.ResponseStatusEnum;
import com.hiyee.platform.service.BaseService;
import com.hiyee.platform.service.LieYingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class LieYingServiceImpl extends BaseService implements LieYingService {
    @Autowired
    AmapLieyingServiceMapper lieYingServiceMapper;
    @Autowired
    AmapLieYingTerminalMapper lieYingTerminalMapper;
    @Autowired
    AmapLieYingTraceMapper amapLieYingTraceMapper;

    @Override
    public Integer insert(LieYingServiceUpdateBO bo) {
        AmapLieyingService service = new AmapLieyingService();
        service.setSid(bo.getSid());
        service.setDesc(bo.getDesc());
        service.setName(bo.getName());
        service.setCreatedAt(new Date());
        service.setUpdatedAt(service.getCreatedAt());
        int result = lieYingServiceMapper.insert(service);
        if(result != 1) {
            GraceException.display(ResponseStatusEnum.FAIL);
        }
        return result;
    }

    @Override
    public PageListResult getList(LieYingServiceListBO bo) {
        Example example = new Example(AmapLieyingService.class);
        Example.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotEmpty(bo.getName())) {
            criteria.andLike("name", bo.getName());
        }
        if(bo.getSid() != null) {
            criteria.andEqualTo("sid", bo.getSid());
        }
        PageHelper.startPage(bo.getPage(), bo.getPageSize());
        List<AmapLieyingService> dataList = lieYingServiceMapper.selectByExample(example);
        return this.generaPageList(dataList);
    }

    @Override
    public void delete(Integer sid) {
        Example example = new Example(AmapLieyingService.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sid", sid);
        int result = lieYingServiceMapper.deleteByExample(example);
        if(result != 1) {
            GraceException.display(ResponseStatusEnum.FAIL);
        }
    }

    @Override
    public Integer update(LieYingServiceUpdateBO bo) {
        // 查询服务
        Example example = new Example(AmapLieyingService.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sid", bo.getSid());
        AmapLieyingService service = lieYingServiceMapper.selectOneByExample(example);
        if(service == null) {
            GraceException.display(ResponseStatusEnum.LIEYING_SERVICE_NOT_FOUND);
        }
        service.setUpdatedAt(new Date());
        service.setName(bo.getName());
        service.setDesc(bo.getDesc());
        service.setStatus(bo.getStatus());
        int result = lieYingServiceMapper.updateByExample(service, example);
        if(result != 1) {
            GraceException.display(ResponseStatusEnum.FAIL);
        }
        return result;
    }

    @Override
    public Integer insertTerminal(LieYingTerminalUpdateBO bo) {
        AmapLieYingTerminal terminal = new AmapLieYingTerminal();
        terminal.setSid(bo.getSid());
        terminal.setIntro(bo.getDesc());
        terminal.setName(bo.getName());
        terminal.setTid(bo.getTid());
        terminal.setCreatedAt(new Date());
        terminal.setUpdatedAt(terminal.getCreatedAt());
        int result = lieYingTerminalMapper.insert(terminal);
        if(result != 1) {
            GraceException.display(ResponseStatusEnum.FAIL);
        }
        return result;
    }

    @Override
    public PageListResult getTerminalList(LieYingTerminalListBO bo) {
        Example example = new Example(AmapLieYingTerminal.class);
        Example.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotEmpty(bo.getName())) {
            criteria.andLike("name", bo.getName());
        }
        if(bo.getSid() != null) {
            criteria.andEqualTo("sid", bo.getSid());
        }
        if(bo.getTid() != null) {
            criteria.andEqualTo("tid", bo.getTid());
        }
        PageHelper.startPage(bo.getPage(), bo.getPageSize());
        List<AmapLieYingTerminal> dataList = lieYingTerminalMapper.selectByExample(example);
        return this.generaPageList(dataList);
    }

    @Override
    public Integer deleteTerminal(Integer tid) {
        Example example = new Example(AmapLieYingTerminal.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tid", tid);
        int result = lieYingTerminalMapper.deleteByExample(example);
        if(result != 1) {
            GraceException.display(ResponseStatusEnum.FAIL);
        }
        return 1;
    }

    @Override
    public Integer insertTrace(LieYingTraceSaveBO bo) {
        AmapLieyingTrace trace = new AmapLieyingTrace();
        trace.setSid(bo.getSid());
        trace.setIntro(bo.getDesc());
        trace.setName(bo.getName());
        trace.setTid(bo.getTid());
        trace.setCreatedAt(new Date());
        trace.setUpdatedAt(trace.getCreatedAt());
        trace.setStatus(true);
        trace.setTrid(bo.getTrid());
        int result = amapLieYingTraceMapper.insert(trace);
        if(result != 1) {
            GraceException.display(ResponseStatusEnum.FAIL);
        }
        return result;
    }

    @Override
    public PageListResult getTraceList(LieYingTraceListBO bo) {
        Example example = new Example(AmapLieyingTrace.class);
        Example.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotEmpty(bo.getName())) {
            criteria.andLike("name", bo.getName());
        }
        if(bo.getSid() != null) {
            criteria.andEqualTo("sid", bo.getSid());
        }
        if(bo.getTid() != null) {
            criteria.andEqualTo("tid", bo.getTid());
        }
        PageHelper.startPage(bo.getPage(), bo.getPageSize());
        List<AmapLieyingTrace> dataList = amapLieYingTraceMapper.selectByExample(example);
        return this.generaPageList(dataList);
    }
}
